<?php

namespace App\Http\Controllers;

use App\Models\Power;
use App\Models\RolePower;
use App\Models\Roles;
use App\Models\RoleUser;
use App\Services\RoleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class RoleController extends Controller
{
    public function power()
    {
        $powers = Power::get();
        $tree = RoleService::createTree($powers);
        return resSucc($tree, '权限树');
    }

    public function roleList()
    {
        $list = Roles::with(['power'])->get(['role_name', 'id', 'updated_at']);
        $list->each->append('power_list');
        return resSucc($list, '权限组列表');
    }

    public function rolePower(Request $request)
    {
        $id = $request->role_id ?? Auth::id();
        $powerId = RoleService::getRoleCode($id);
        return resSucc($powerId, '权限列表');
    }

    public function roleCreate(Request $request)
    {
        $role_name = $request->role_name;

        if (!$role_name) {
            return resFail(null, '参数错误');
        }

        $role = new Roles;
        $role->school_id = $request->school_id;
        $role->campus_id = $request->campus_id;
        $role->role_name = $role_name;
        $role->save();
        return resSucc($role->id, '创建成功');
    }

    public function roleUpdate(Request $request)
    {

        $role_name = $request->role_name;
        $role_id = $request->role_id;

        if (!$role_name || !$role_id) {
            return resFail(null, '参数错误');
        }

        $role = Roles::find($role_id);
        $role->role_name = $role_name;
        $role->save();
        return resSucc($role, '修改成功');
    }

    public function roleDelete(Request $request)
    {

        $role_id = $request->role_id;

        if (!$role_id) {
            return resFail(null, '参数错误');
        }

        if (RoleUser::where('role_id', $role_id)->exists()) {
            return resFail(null, '当前权限组下有用户，暂无法删除');
        }

        DB::beginTransaction();

        try {
            Roles::where('id', $role_id)->delete();

            RolePower::where('role_id', $role_id)->delete();

            DB::commit();

            return resSucc(null, '删除成功');
        } catch (\Exception $e) {
            DB::rollback();
            return resFail($e, "删除失败");
        } catch (\Throwable $e) {
            DB::rollback();
            return resFail($e, "删除失败");
        }
    }

    public function rolePowerUpdate(Request $request)
    {
        $checked = $request->checked;
        $ids = Power::whereIn('code', $checked)->get()->pluck('id');
        $role_id = $request->role_id;

        DB::beginTransaction();

        try {
            // 删除所有权限 关系，
            RolePower::where('role_id', $role_id)->delete();
            foreach ($ids as $id) {
                $rolePowe = new RolePower;
                $rolePowe->role_id = $role_id;
                $rolePowe->power_id = $id;
                $rolePowe->save();
            }
            DB::commit();
            return resSucc(null, '修改成功');
        } catch (\Exception $e) {
            DB::rollback();
            return resFail($e, "创建失败");
        } catch (\Throwable $e) {
            DB::rollback();
            return resFail($e, "创建失败");
        }
    }
}
